using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Xyne.Utilities
{
    public class MathManager
    {
        public static float CalculateDegreeFromVector(Vector2 inVector)
        {
            float degree = 0;

            if (inVector.X != 0)
            {
                if (inVector.Y == 0)
                {
                    if (inVector.X > 0)
                    {
                        degree = 90;
                    }
                    else
                    {
                        degree = 270;
                    }
                }
                else
                {
                    degree = MathHelper.ToDegrees((float)Math.Atan(inVector.Y / inVector.X));
                    if (inVector.X > 0)
                    {
                        if (inVector.Y > 0)
                        {
                            degree = 90 - degree;
                        }
                        else
                        {
                            degree = Math.Abs(degree) + 90;
                        }
                    }
                    else
                    {
                        if (inVector.Y > 0)
                        {
                            degree = Math.Abs(degree) + 270;
                        }
                        else
                        {
                            degree = 270 - degree;
                        }
                    }

                }
            }
            else
            {
                if (inVector.Y < 0)
                {
                    degree = 180;
                }
            }

            return degree;
        }

        public static Vector2 VectorFromDegreeRadius(float degree, float radius)
        {

            double x = Math.Sin(MathHelper.ToRadians(degree))*radius;
            double y = Math.Cos(MathHelper.ToRadians(degree))*radius;

            return new Vector2((float)x, (float)y);
        }

        public static float DegreeCompare(float degree1, float degree2)
        {
            float degreeDiff = Math.Abs(degree1 - degree2);
            if (degreeDiff > 180)
            {
                degreeDiff = 360 - degreeDiff;
            }

            return degreeDiff;
        }
    }
}
